﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GeneratorLib.DB;
using GeneratorLib.DB.Connectios;
using GeneratorLib.DB.Schemas;

namespace GeneratorCode.Pasys
{

    public class MVCModel
    {
        public static string getSingleDeclare(FieldInfo field)
        {
            return string.Format("public {0} {1} {{get;set;}}", field.FieldType.MapName, field.FieldName);
        }

        public static string getSingleDeclareForSearchModel(FieldInfo field)
        {
            string type = field.FieldType.MapName;
            if (field.Nullabled && type.ToLower().CompareTo("datetime") == 0)
                type = "string";

            return string.Format("public {0} {1} {{get;set;}}", type, field.FieldName);
        }

        /// <summary>
        /// declare variable and get value from InParam.Param
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public static string getSingleConvert(FieldInfo field, string format)
        {
            //Converts.ToTryInt({0},0)
            string method = Pasys.Converts.ConvertsMethodName(field);
            string fName = string.Format(format, field.FieldName);
            return string.Format(method, fName);
        }

        public static string getSingleConvertForSearchModel(FieldInfo field, string format)
        {
            string method = Pasys.Converts.ConvertsMethodName(field, true);
            string fName = string.Format(format, field.FieldName);
            return string.Format(method, fName);
        }

    }

    public class MVCView
    {
        public static string getSingleHTMLDeclare(FieldInfo field)
        {
            string htmlType = "";
            var res = from s in field.Table.Connection.Relations
                      where
                          s.ForeignTable.TableName.ToLower().CompareTo(field.Table.TableName.ToLower()) == 0
                      select s;

            bool isForeignKey = false;
            TableInfo t = null;
            foreach (var r in res)
            {
                if (r.PrimaryTable.KeyFieldName.ToLower().CompareTo(field.FieldName.ToLower()) == 0)
                {
                    isForeignKey = true;
                    t = field.Table.Connection.FindTable(r.PrimaryTable.TableName);
                    break;
                }
            }


            switch (field.FieldType.MapName.ToLower())
            {
                case "int":
                    if (!isForeignKey)
                        htmlType = string.Format("<%= Html.TextBoxFor(m => m.{0},new {{ @class = \"NumText\", maxLength = \"10\" }})%>", field.FieldName);
                    else
                        htmlType = string.Format("<%= Html.DropDownListFor(m => m.{0}, new SelectList(Model.{1}, \"Value\", \"Text\", Model.{0}))%>", field.FieldName, t.EntityListName);

                    break;
                case "datetime":
                    htmlType = string.Format("<%= Html.TextBoxFor(m => m.{0},new {{ @class = \"datepicker\", maxLength = \"10\" }})%>", field.FieldName);
                    break;
                case "decimal":
                    htmlType = string.Format("<%= Html.TextBoxFor(m => m.{0},new {{ @class = \"NumText\", maxLength = \"10\" }})%>", field.FieldName);
                    break;
                case "bool":
                    htmlType = string.Format("<%= Html.CheckBoxFor(m => m.{0})%>", field.FieldName);
                    break;
                case "string":
                    htmlType = string.Format("<%= Html.TextBoxFor(m => m.{0},new {{ @class = \"Fullwidth\", maxLength = \"{1}\" }})%>", field.FieldName, field.FieldSize);
                    break;
                case "double":
                    htmlType = string.Format("<%= Html.TextBoxFor(m => m.{0},new {{ @class = \"NumText\", maxLength = \"10\" }})%>", field.FieldName);
                    break;
                default:
                    htmlType = string.Format("<%= Html.TextBoxFor(m => m.{0},new {{ @class = \"NumABC\", maxLength = \"{1}\" }})%>", field.FieldName, field.FieldSize);
                    break;
            }
            return htmlType;
        }

        public static string getSingleTextNavString(FieldInfo field)
        {
            string textNav = "";

            var res = from s in field.Table.Connection.Relations
                      where
                          s.ForeignTable.TableName.ToLower().CompareTo(field.Table.TableName.ToLower()) == 0
                      select s;

            bool isForeignKey = false;
            TableInfo t = null;
            foreach (var r in res)
            {
                if (r.PrimaryTable.KeyFieldName.ToLower().CompareTo(field.FieldName.ToLower()) == 0)
                {
                    isForeignKey = true;
                    t = field.Table.Connection.FindTable(r.PrimaryTable.TableName);
                    break;
                }
            }


            switch (field.FieldType.MapName.ToLower())
            {
                case "int":
                    if (!isForeignKey)
                        textNav = "半角英数字で入力してください。";
                    else
                        textNav = "";
                    break;
                case "datetime":
                    textNav = "半角英数字で入力してください。";
                    break;
                case "decimal":
                    textNav = "半角英数字で入力してください。";
                    break;
                case "bool":
                    textNav = "半角英数字で入力してください。";
                    break;
                case "string":
                    textNav = "全角文字で入力して下さい。";
                    break;
                case "double":
                    textNav = "半角英数字で入力してください。";
                    break;
                default:
                    textNav = "半角英数字で入力してください。";
                    break;
            }
            return textNav;
        }


    }
}
